3.12 How to send data to a server script?

  1. How to send data to the server?
    • Using a <form>
      • Attributes
        • method: post, get; the method how to send data
        • action: the URL of a server script that will receive data
        • [target]: optional; window (or iframe) to display the web content coming back from the action server script
    • <input>, <checkbox>, <radio>, <textarea>, <select>, ... within a form SHOULD have name and value. These pairs of name and value will be submitted to the URL specified in form-action, when the <input type='submit'> or <button> within the form is clicked.
    • What if name is omitted?
      The data will not be submitted. Why?
    • How are the HTTP get and post methods different? Read all in HTML Forms.
    • Can a <form> be submitted to another client-side web application?
    • Here is an example.
      <form ???='get' ???='controller.php'>
          <input type='hidden' name='page' value='StartPage'>  <!-- hidden: not visible to the user -->
          <input type='???' name='command' value='SignIn'>
          Username:
          <input type='text' name='username' required><br>
          Password:
          <input type='???' name='password' ???><br>
          <input type='???' value='Cancel'> <!-- regular button -->  
          <input type='???' ???='Submit'> <!-- submit button -->
      </form>
      
      // controller.php; server side script; Of course we will discuss PHP very soon.
      <?php
          echo $_GET['page'] . '<br>';  // It should be ???.
          echo $_GET['command'] . '<br>';  // It should be ???.
          echo ???['username'] . '<br>';
          echo ???['password'] ??? '<br>';
      ?>
      
    • Trial 1: Let's try to send username and password to //cs.tru.ca/~mlee/comp3540/Software/test_display_inputs.php. You may use the form in the above example.


    • We will discuss later how to receive data sent from the client when we discuss about PHP.
    • There is another way to send data to the server. It is called AJAX, and we will discuss about it later.

  2. Learning outcomes
    • How to send data to the server.
    • Explain how the HTTP get and post methods are different.